package com.xuyiwei.xblog.modular.system.controller;

import com.xuyiwei.xblog.business.system.model.Template;
import com.xuyiwei.xblog.business.system.service.ITemplateService;
import com.xuyiwei.xblog.core.base.controller.BaseController;
import com.xuyiwei.xblog.core.base.response.Result;
import com.xuyiwei.xblog.core.exception.BizExceptionEnum;
import com.xuyiwei.xblog.core.exception.GunsException;
import com.xuyiwei.xblog.core.util.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.xuyiwei.xblog.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;

/**
 * 系统模板控制器
 *
 * @author fengshuonan
 * @Date 2018-12-02 16:20:11
 */
@Controller
@RequestMapping("/template")
public class TemplateController extends BaseController {

    private String PREFIX = "/system/template/";

    @Autowired
    private ITemplateService templateService;

    /**
     * 跳转到系统模板首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "template.html";
    }

    /**
     * 跳转到添加系统模板
     */
    @RequestMapping("/template_add")
    public String templateAdd() {
        return PREFIX + "template_add.html";
    }

    /**
     * 跳转到修改系统模板
     */
    @RequestMapping("/template_update/{templateId}")
    public String templateUpdate(@PathVariable Integer templateId, Model model) {
        Template template = templateService.selectById(templateId);
        model.addAttribute("item",template);
        LogObjectHolder.me().set(template);
        return PREFIX + "template_edit.html";
    }

    /**
     * 获取系统模板列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result<List<Template>> list(String condition) {
        Result<List<Template>> result = Result.instance();
        try{
            List<Template> list = templateService.selectList(null);
            result.setData(list);
        }catch (Exception e){
            result.setError("500","获取文章标签出错");
        }
        return result;
    }

    /**
     * 新增系统模板
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result<Boolean> add(Template template) {
        Result<Boolean> result = Result.instance();
        try{
            templateService.insert(template);
            result.setData(true);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 删除系统模板
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(required = true, value = "ids[]") List<Integer> ids) {
        Result<Boolean> result = Result.instance();
        try{
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.templateService.deleteBatchIds(ids);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 修改系统模板
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result<Boolean> update(Template template) {
        Result<Boolean> result = Result.instance();
        try{
            templateService.updateById(template);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 系统模板详情
     */
    @RequestMapping(value = "/detail/{templateId}")
    @ResponseBody
    public Result<Template> detail(@PathVariable("templateId") Integer templateId) {
        Result<Template> result = Result.instance();
        try{
            Template template = templateService.selectById(templateId);
            result.setData(template);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }
}
